package com.sj.utils;

import cn.hutool.core.util.StrUtil;
import com.sj.common.exception.CustomException;
import com.sj.common.lang.Const;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xiaoqi
 * Date: 2022-03-10
 * Time: 下午 14:43
 * Description: 自定义工具类
 *
 * @author xiaoqi
 * @since 2022-03-10
 */
@Component
public class MineUtils {

    @Autowired
    private JwtUtils jwtUtils;

    @Autowired
    private RedisUtils redisUtils;

    /**
     * 合并多个集合
     *
     * @param lists
     * @return
     */
    public List concatList(List... lists) {
        List<Object> list = new ArrayList<>();
        for (List<Object> tempList : lists) {
            for (Object temp : tempList) {
                list.add(temp);
            }
        }
        return list;
    }

    /**
     * 根据token 获取用户id
     * @return
     */
    public Integer getUidByToken() {
        Claims claims = jwtUtils.checkToken();
        if (claims != null) {
            return Integer.parseInt(claims.getSubject().split("-")[0]);
        }
        return null;
    }

    /**
     * 检查验证码
     * @param request
     */
    public void checkCaptcha(HttpServletRequest request) {
        String key = request.getParameter("key");
        String code = request.getParameter("code");
        this.validate(key, code);
    }

    /**
     * 判断验证码是否正确
     * @param key
     * @param code
     */
    private void validate(String key, String code) {
        if (StrUtil.isBlankOrUndefined(key) || StrUtil.isBlankOrUndefined(code)) {
            throw new CustomException("请获取验证码");
        }
        if (!redisUtils.hHasKey(Const.CAPTCHA_KEY, key)) {
            throw new CustomException("验证码已过期，请重新获取");
        }
        String redisCode = String.valueOf(redisUtils.hget(Const.CAPTCHA_KEY, key));
        if (!redisCode.equalsIgnoreCase(code)) {
            redisUtils.hdel(Const.CAPTCHA_KEY, key);
            throw new CustomException("验证码错误");
        }
    }


    /**
     * Description: 判断OSS服务文件上传时文件的contentType
     *
     * @param FilenameExtension 文件后缀
     * @return String
     */
    public String getContentType(String FilenameExtension) {
        if (FilenameExtension.equalsIgnoreCase(".bmp")) {
            return "image/bmp";
        }
        if (FilenameExtension.equalsIgnoreCase(".gif")) {
            return "image/gif";
        }
        if (FilenameExtension.equalsIgnoreCase(".jpeg") ||
                FilenameExtension.equalsIgnoreCase(".jpg") ||
                FilenameExtension.equalsIgnoreCase(".png")) {
            return "image/jpg";
        }
        if (FilenameExtension.equalsIgnoreCase(".tif") ||
                FilenameExtension.equalsIgnoreCase(".tiff")) {
            return "image/tiff";
        }
        throw new CustomException("文件格式错误，请上传.jpg,.jpeg,.png,.gif,.tiff类型的文件");
    }
}